package de.hmm.hwip.messagehandler.service;

import java.net.Socket;

import org.json.JSONObject;
import org.osgi.framework.BundleContext;

import android.os.Bundle;

import de.hmm.hwip.general.IMessageContent;
import de.hmm.hwip.messagehandler.api.IMessageHandler;

/**
 *  Implementation of the Messagehandler service.
 * 	Service generates a new instance of the needed 
 *  Messagetype unit via FQD
 * 
 * @author johannes mendel
 * @version 26.09.2012
 *
 */
public class MessageHandlerImpl implements IMessageHandler {
	/**
	 * Starting the Messageaction.
	 * They are called from a factory of different message handling services.
	 * Every class is generated and started as single thread
	 * so that multiple actions are able to perform simultaneous
	 * 
	 * @param FQD - URL of the Full Quallified Domain of the service - e.g. de.hmm.hwip.SetBasics
	 * @param context - osgi bundle context
	 * @param payload - JsonObject with payload data received from end-device
	 * @param connection - socket connection of end-device
	 * @param securityFlag - boolean flag if AES is used
	 */
	
	@SuppressWarnings("rawtypes")
	@Override
	public void startAction(String fqd, BundleContext context, JSONObject payload, Socket connection, boolean securityFlag) {
		
		try {
			//generate Class out of the Factory
			Class c = Class.forName(fqd);
			IMessageContent msgUnit = (IMessageContent) c.newInstance();
			
			// set Messageparameters
			msgUnit.setBundleContext(context);
			msgUnit.setPayload(payload);
			msgUnit.setConnection(connection);
			msgUnit.setSecurityFlag(securityFlag);
			
			/* starting the preparation thread
			 this one call the data base request and the performs 
			 the call back to the end-device */
			new Thread(msgUnit).start();
			
		} catch (Exception e) {
			System.out.println("Error at creating MessageType unit -> " + e);
			
			// when class is not found via FQDN, send feedback to end device
			/*
			ErrorPreparationUnit error = new ErrorPreparationUnit();
			error.setBundleContext(context);
			error.setConnection(connection);
			error.setMap(map);
			error.setSecurityFlag(securityFlag);
			
			new Thread(error).start();
			*/
		} 
	}

	/**
	 * Starting the Messageaction.
	 * They are called from a factory of different message handling services.
	 * Every class is generated and started as single thread
	 * so that multiple actions are able to perform simultaneous
	 * 
	 * @param FQD - URL of the Full Quallified Domain of the service - e.g. de.hmm.hwip.SetBasics
	 * @param radio - type of radio
	 * @param deviceMessage - content of message received by system device

	 */
	@SuppressWarnings("rawtypes")
	@Override
	public void startAction(String fqd, int radio, Bundle deviceMessage, BundleContext context) {
		
		try {
		Class c = Class.forName(fqd);
		IMessageContent msgUnit = (IMessageContent) c.newInstance();
		
		JSONObject payload = new JSONObject();
		payload.put("radio", radio);
		payload.put("service", fqd);
		
		for(String key : deviceMessage.keySet()){
			payload.put(key, deviceMessage.get(key));
		}
		System.out.println("payload: " + payload);
		msgUnit.setPayload(payload);
		msgUnit.setBundleContext(context);
		
		/* starting the preparation thread
		 this one call the data base request and the performs 
		 the call back to the end-device */
		new Thread(msgUnit).start();
		
		} catch (Exception e) {
			System.out.println("Error at creating MessageType unit -> " + e);
			
			// when class is not found via FQDN, send feedback to end device
			/*
			ErrorPreparationUnit error = new ErrorPreparationUnit();
			error.setBundleContext(context);
			error.setConnection(connection);
			error.setMap(map);
			error.setSecurityFlag(securityFlag);
			
			new Thread(error).start();
			*/
		} 

		
	}

}
